package com.example.archive.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.archive.entity.ArchiveDestructionRequest;
import com.example.archive.entity.User;
import com.example.archive.service.ArchiveDestructionRequestService;
import com.example.archive.service.UserService;
import com.example.archive.util.PageParam;
import com.example.archive.util.Result;
import com.example.archive.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/archive-destruction")
public class ArchiveDestructionRequestController {

    @Autowired
    private ArchiveDestructionRequestService destructionRequestService;

    @Autowired
    private UserService userService;

    @PostMapping("/submit")
    public Result<Boolean> submitRequest(@RequestBody ArchiveDestructionRequest request) {
        Integer currentUserId = UserContext.getUserId();
        if (currentUserId == null) {
            return Result.fail(401, "用户未登录");
        }
        // 根据需求文档，销毁申请由档案管理员发起
        Result<User> userResult = userService.getUserById(currentUserId);
        if (userResult.getCode() != 200 || userResult.getData() == null || !"档案管理员".equals(userResult.getData().getRole())) {
            return Result.fail(403, "只有档案管理员才能提交销毁申请");
        }
        request.setRequesterId(currentUserId);
        return destructionRequestService.submitDestructionRequest(request);
    }

    @GetMapping("/list")
    public Result<IPage<ArchiveDestructionRequest>> listRequests(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String status,
            @RequestParam(required = false) Integer requesterId,
            @RequestParam(required = false) Integer approverId,
            @RequestParam(required = false) String search) {

        Integer currentUserId = UserContext.getUserId();
        if (currentUserId == null) {
            return Result.fail(401, "用户未登录");
        }

        Result<User> userResult = userService.getUserById(currentUserId);
        if (userResult.getCode() != 200 || userResult.getData() == null) {
            return Result.fail(401, "无法获取用户信息");
        }
        String userRole = userResult.getData().getRole();

        Integer effectiveRequesterId = requesterId;
        Integer effectiveApproverId = approverId;

        // 根据需求文档，普通员工无权查看销毁申请
        if ("普通员工".equals(userRole)) {
            return Result.fail(403, "普通员工无权查看销毁申请");
        } else if ("档案管理员".equals(userRole)) {
            // 档案管理员可以查看所有销毁申请
        } else if ("企业领导".equals(userRole)) {
            // 企业领导可以查看所有销毁申请
        }

        PageParam pageParam = new PageParam(pageNum, pageSize, search);
        return destructionRequestService.listDestructionRequests(pageParam, status, effectiveRequesterId, effectiveApproverId);
    }

    @PostMapping("/approve/{destructionId}")
    public Result<Boolean> approveRequest(@PathVariable Integer destructionId, @RequestBody(required = false) String notes) {
        Integer approverId = UserContext.getUserId();
        if (approverId == null) {
            return Result.fail(401, "用户未登录");
        }
        // 只有企业领导才能批准
        Result<User> userResult = userService.getUserById(approverId);
        if (userResult.getCode() != 200 || userResult.getData() == null || !"企业领导".equals(userResult.getData().getRole())) {
            return Result.fail(403, "只有企业领导才能批准销毁申请");
        }
        return destructionRequestService.approveDestructionRequest(destructionId, approverId, notes);
    }

    @PostMapping("/reject/{destructionId}")
    public Result<Boolean> rejectRequest(@PathVariable Integer destructionId, @RequestBody(required = false) String notes) {
        Integer approverId = UserContext.getUserId();
        if (approverId == null) {
            return Result.fail(401, "用户未登录");
        }
        // 只有企业领导才能拒绝
        Result<User> userResult = userService.getUserById(approverId);
        if (userResult.getCode() != 200 || userResult.getData() == null || !"企业领导".equals(userResult.getData().getRole())) {
            return Result.fail(403, "只有企业领导才能拒绝销毁申请");
        }
        return destructionRequestService.rejectDestructionRequest(destructionId, approverId, notes);
    }

    @PostMapping("/execute/{destructionId}")
    public Result<Boolean> executeDestruction(@PathVariable Integer destructionId, @RequestBody(required = false) String destructionProcessRecord) {
        Integer executorId = UserContext.getUserId();
        if (executorId == null) {
            return Result.fail(401, "用户未登录");
        }
        // 只有档案管理员才能执行销毁
        Result<User> userResult = userService.getUserById(executorId);
        if (userResult.getCode() != 200 || userResult.getData() == null || !"档案管理员".equals(userResult.getData().getRole())) {
            return Result.fail(403, "只有档案管理员才能执行销毁操作");
        }
        return destructionRequestService.executeDestruction(destructionId, executorId, destructionProcessRecord);
    }

    @GetMapping("/detail/{destructionId}")
    public Result<ArchiveDestructionRequest> getRequestDetail(@PathVariable Integer destructionId) {
        return destructionRequestService.getDestructionRequestById(destructionId);
    }
} 